import { httpPost } from './http.js';

(() => {
  const oApp = document.querySelector('#app');
  const oRemoveBtn = oApp.querySelector('#removeBtn');

  const init = () => {
    bindEvent();
  };

  function handleRemoveBtnClick(e) {
    const tar = e.target;
    const id = tar.dataset.id;
    removeTodo(id).then((res) => {
      if (res.code === 0) {
        location.href = 'http://localhost:8000';
      }
    });
  }

  async function removeTodo(id) {
    return await httpPost('/remove_todo', { id });
  }

  function bindEvent() {
    oRemoveBtn.addEventListener('click', handleRemoveBtnClick, false);
  }

  init();
})();
